# coding=utf-8
import importlib
import os
import sys
import re
from pathlib import Path


class Import_modules:
    def __init__(self):
        self.all_modules = {}
        self.import_function()

    def recursive_get_python_file(self,path):
        global modules
        names_list = os.listdir(path)
        shell_command_get_py = "ls " + path + " | grep -i .py$"
        python_files_name_list = [i.split(".")[0] for i in os.popen(shell_command_get_py).read().split("\n\r")[0].strip().split("\n")]
        for py_name in python_files_name_list:
            if py_name:
                py_path = os.path.join(path,py_name)
                moduls_path = py_path.split(os.path.dirname(sys.argv[0]).split("/")[-1]+"/")[-1]
                moduls_path = moduls_path.replace("/",".")
        
                moduleArray = moduls_path.split(".")

                moduls_path = "Function." + moduleArray[-1]
                print("导入的文件：",moduls_path)
                # self.all_modules[py_name] = importlib.import_module(moduls_path)
                self.all_modules[py_name] = importlib.import_module(moduls_path)
            
        for name_file in names_list:
            name_directory = os.path.join(path,name_file)
            
            if os.path.isdir(name_directory):
                self.recursive_get_python_file(name_directory)
    def import_function(self):
        # parent_directory = os.path.abspath(os.path.dirname(__file__))
        parent_directory = os.path.join(Path(__file__).parent.parent)
        print("------------------")
        print(parent_directory)
        function_path = os.path.join(parent_directory,"Function")
        print("Function file path:",function_path)
        self.recursive_get_python_file(function_path)
 

if __name__ == "__main__":
    functions = Import_modules()
    all_function = functions.all_modules
    # all_function["outputLog222"].function_template["logInfo"]("111","22")
    # all_function["template"].function_dic["logInfo"]("111")
    result = all_function["function_dic"].function_dic["testReturnStr"]()
    print(result)

